package com.example.libary.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.libary.common.Result;
import com.example.libary.entity.Book;
import com.example.libary.entity.BorrowRecord;
import com.example.libary.entity.User;
import com.example.libary.service.BookService;
import com.example.libary.service.BorrowRecordService;
import com.example.libary.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 统计控制器
 */
@Tag(name = "统计管理", description = "系统统计数据相关接口")
@RestController
@RequestMapping("/statistics")
@RequiredArgsConstructor
public class StatisticsController {

    private final BookService bookService;
    private final UserService userService;
    private final BorrowRecordService borrowRecordService;

    /**
     * 获取系统统计概览
     */
    @Operation(summary = "获取系统统计概览", description = "获取图书、用户、借阅等统计数据")
    @GetMapping("/overview")
    public Result<Map<String, Object>> getOverview() {
        Map<String, Object> statistics = new HashMap<>();
        
        // 图书总数
        long totalBooks = bookService.count();
        statistics.put("totalBooks", totalBooks);
        
        // 注册用户总数
        long totalUsers = userService.count();
        statistics.put("totalUsers", totalUsers);
        
        // 当前借阅中的图书数量
        QueryWrapper<BorrowRecord> borrowingQuery = new QueryWrapper<>();
        borrowingQuery.eq("status", "0"); // 借阅中状态
        long activeBorrows = borrowRecordService.count(borrowingQuery);
        statistics.put("activeBorrows", activeBorrows);
        
        // 逾期图书数量
        QueryWrapper<BorrowRecord> overdueQuery = new QueryWrapper<>();
        overdueQuery.eq("status", "2") // 逾期状态
                   .or()
                   .eq("status", "0") // 借阅中但已过期
                   .lt("due_date", LocalDateTime.now());
        long overdueBooks = borrowRecordService.count(overdueQuery);
        statistics.put("overdueBooks", overdueBooks);
        
        // 可借图书数量
        QueryWrapper<Book> availableQuery = new QueryWrapper<>();
        availableQuery.gt("available", 0);
        long availableBooks = bookService.count(availableQuery);
        statistics.put("availableBooks", availableBooks);
        
        return Result.success(statistics);
    }

    /**
     * 获取图书分类统计
     */
    @Operation(summary = "获取图书分类统计", description = "按分类统计图书数量")
    @GetMapping("/categories")
    public Result<Map<String, Object>> getCategoryStatistics() {
        // 这里可以添加分类统计逻辑
        Map<String, Object> categoryStats = new HashMap<>();
        categoryStats.put("message", "分类统计功能待实现");
        return Result.success(categoryStats);
    }

    /**
     * 获取借阅趋势统计
     */
    @Operation(summary = "获取借阅趋势统计", description = "获取最近30天的借阅趋势")
    @GetMapping("/trends")
    public Result<Map<String, Object>> getBorrowTrends() {
        // 这里可以添加趋势统计逻辑
        Map<String, Object> trends = new HashMap<>();
        trends.put("message", "借阅趋势统计功能待实现");
        return Result.success(trends);
    }
} 